package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.mapper.StrategyDetailMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyContentMapper contentMapper;

    @Override
    public void save(StrategyDetail strategyDetail) {
        strategyDetailMapper.insert(strategyDetail);
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyDetail> list = strategyDetailMapper.queryForList(qo);
        return new PageInfo(list);
    }


    @Override
    public void delete(Long id) {
        strategyDetailMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail) {
        // 设置默认值
        if(strategyDetail.getState() == StrategyDetail.STATE_RELEASE){
            strategyDetail.setReleaseTime(new Date());// 发布时间
        }

        // 默认的序号
        if (strategyDetail.getSequence() == null) {
            strategyDetail.setSequence(strategyDetailMapper.getMaxSquence(strategyDetail.getCatalog().getId()) + 1);
        }

        StrategyContent strategyContent = strategyDetail.getContent();
        if (strategyDetail.getId() != null) {
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            // 更新攻略内容
            strategyContent.setId(strategyDetail.getId());
            contentMapper.updateByPrimaryKey(strategyContent);
        }else {
            strategyDetail.setCreateTime(new Date()); // 创建时间
            strategyDetailMapper.insert(strategyDetail);
            // 保存攻略内容
            strategyContent.setId(strategyDetail.getId());
            contentMapper.insert(strategyContent);
        }
    }

    @Override
    public StrategyDetail getContentByStrategyId(Long detailId) {
        StrategyContent content = contentMapper.selectByPrimaryKey(detailId);
        StrategyDetail strategyDetail = strategyDetailMapper.selectByPrimaryKey(detailId);
        strategyDetail.setContent(content);
        return strategyDetail;
    }

    @Override
    public List<StrategyDetail> selectAll() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public void update(StrategyDetail strategyDetail) {
        strategyDetailMapper.updateByPrimaryKey(strategyDetail);
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

}
